home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: VanCLy@ix.netcom.com@ix.netcom.com (Van Chinh Ly)
- Newsgroups: comp.lang.c++
- Subject: Re: How do I put DIFFERENT classes in the same linked list?
- Date: 8 Apr 1996 06:44:35 GMT
- Organization: Netcom
- Message-ID: <4kackj$7is@reader2.ix.netcom.com>
- References: <4ka938$bj6@wintermute.ecs.fullerton.edu>
- Reply-To: VanCLy@ix.netcom.com
- NNTP-Posting-Host: sfo-ca11-12.ix.netcom.com
- X-NETCOM-Date: Sun Apr 07 11:44:35 PM PDT 1996
- X-Newsreader: IBM NewsReader/2 v1.2.5
-
- In <4ka938$bj6@wintermute.ecs.fullerton.edu>, grosin@titan (Gil Rosin) writes:
- >...
- >
- >#include <iostream.h>
- >
- >class A
- > {
- > public:
- > A *next;
- > A *prev;
- > void Print();
- > };
- >
- >class Demo
- > {
- > public:
- > void Insert(A *Temp);
- > void Insert2(A *Temp);
- > void Test(void);
- > A *First;
- > };
- >
- >class B : public A
- > {
- > void Print();
- > };
- >
- >class C : public A
- > {
- > void Print();
- > };
- >
- >main()
- > {
- > Demo *T = new Demo;
- > A *Test = new B;
- > T->Insert(Test);
- > Test = new C;
- > T->Insert2(Test);
- > T->Test();
- > return 0;
- > }
- >
- >void B::Print()
- > {
- > cout << "I am in class B" << '\n';
- > }
- >
- >void C::Print()
- > {
- > cout << "I am in class C" << '\n';
- > }
- >
- >...
- >
- >Now, as you can see, I want to have a linked list that contains EITHER
- >B or C Items, but in the void Demo::Test() function, I want to be able to
- >just call them blindly with out knowing which is which. When I compile this
- >I get NO errors, but when I run it the two lines that print out are the
- >class A print function. It should print out the class B and then the class C.
- >
- >How can I do this? I am not worrying about constructors/destructors etc
- >right now, just worrying about how to get it to work.
- >
-
- Is this homework? :)
-
- Check out "virtual functions."
-
-
- Van
-
-